package com.atguigu.shangrongbao.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.shangrongbao.core.listener.DictExeclDtoListener;
import com.atguigu.shangrongbao.core.pojo.dto.DictListDto;
import com.atguigu.shangrongbao.core.pojo.dto.ExcelDictDto;
import com.atguigu.shangrongbao.core.pojo.entity.Dict;
import com.atguigu.shangrongbao.core.mapper.DictMapper;
import com.atguigu.shangrongbao.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author Author
 * @since 2021-08-10
 */
@Slf4j
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public void parseExcelData(InputStream inputStream) {
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(inputStream, ExcelDictDto.class, new DictExeclDtoListener(baseMapper)).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);

        } catch (Exception e) {
            log.info(e.getMessage(), e);
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR);
        } finally {
            if (excelReader != null) {
                excelReader.finish();
            }
        }
    }

    @Override
    public List<DictListDto> getAllDict(Long id) {

        //如果传了id，根据id查询数据
        List<Dict> dictList = baseMapper.selectList(new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, id));

        List<DictListDto> dictDtos = new ArrayList<>();
        for (Dict dict : dictList) {
            DictListDto dictDto = new DictListDto();
            //把数据拷贝到dictDto
            BeanUtils.copyProperties(dict, dictDto);
            //判断是否还有子集
            Integer count = baseMapper.selectCount(new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, dictDto.getId()));
            dictDto.setHasChildren(count > 0);
            dictDtos.add(dictDto);
        }
        log.info("拷贝后的所有数据:{}", dictDtos);

        return dictDtos;
    }

    @Override
    public List<ExcelDictDto> getAllDict() {
        List<Dict> dictList = baseMapper.selectList(null);

        log.info("数据字典所有数据：{}", dictList);

        ArrayList<ExcelDictDto> dictDtos = new ArrayList<>();
        for (Dict dict : dictList) {
            ExcelDictDto excelDictDto = new ExcelDictDto();
            //把数据拷贝到excelDictDto
            BeanUtils.copyProperties(dict, excelDictDto);
            dictDtos.add(excelDictDto);
        }
        log.info("拷贝后的所有数据:{}", dictDtos);

        return dictDtos;
    }
}
